import numpy as np
import pandas as pd
import matplotlib.pyplot as plot
import plotly.graph_objs as go
import plotly
plotly.offline.init_notebook_mode()
from linear_regression import LinearRegression
#dataset url https://archive.ics.uci.edu/static/public/715/lt+fs+id+intrusion+detection+in+wsns.zip
data = pd.read_csv('MLbase\dataset\data.csv')
print(data)
train_set = data.sample(frac=0.8)
test_set = data.drop(train_set.index)

x_train = train_set[['Sensing Range','Number of Sensor nodes']].values
y_train = train_set[['Number of Barriers']].values

x_test = test_set[['Sensing Range','Number of Sensor nodes']].values
y_test = test_set[['Number of Barriers']].values

num_iterations = 1000
learning_rate = 0.01

linearRegression = LinearRegression(x_train,y_train)
cost_history = linearRegression.train(learning_rate,num_iterations)
print("Beginning Loss:",cost_history[0])
print("End Loss:",cost_history[-1])

plot.plot(range(num_iterations),cost_history)
plot.xlabel('iter_num')
plot.ylabel('loss')
plot.show()

#获得均匀序列
#行向量转列向量 arr = arr[...,None]
#x_predictions = np.linspace(x_train.min(),x_train.max(),100)[...,None]
#y_predictions = linearRegression.predict(x_predictions)
#plot.plot(x_predictions,y_predictions,'r')

training_trace = go.Scatter3d(
    x = x_train[:,0].flatten(),
    y = x_train[:,1].flatten(),
    z = y_train.flatten(),
    name = 'train_set',
    mode = 'markers',
    marker={
        'size':10,
        'opacity':1,
        'line':{
            'color':'rgb(255,255,255)',
            'width':1
        }
    }
)
layout = go.Layout(
    title='data_set',
    scene={
        'xaxis':{'title':'x1'},
        'yaxis':{'title':'x2'},
        'zaxis':{'title':'y'},
    },
    margin={'l':0,'r':0,'b':0,'t':0}
)
plot_data = [training_trace]
figure = go.Figure(data=plot_data,layout=layout)
plotly.offline.plot(figure)







